﻿/******************************************************************************
* file  : console.c
* creat : apleilx
* data  : 2021/01/20
*******************************************************************************/
#include "os_obj.h"
#include "sys_task.h"
#include "pincfg.h"
#include "com_obj.h"
#include "dataproc.h"
#include "common.h"
#include "simp_txt_analysis.h"
#include "password.h"

uint32_t cmd_back_dat[16];
void cmd_callback(uint16_t cmd_site, const char * txt_src, uint16_t len);

#define CMD_ATTR_ADMIN         1
#define CMD_ATTR_FACTORY       (CMD_ATTR_ADMIN + 1)
#define CMD_ATTR_PASSWORD      (CMD_ATTR_FACTORY + 1)
#define CMD_ATTR_DEV_ID        (CMD_ATTR_PASSWORD + 1)
#define CMD_ATTR_DEV_NAME      (CMD_ATTR_DEV_ID + 1)
#define CMD_ATTR_DEV_CLR       (CMD_ATTR_DEV_NAME + 1)
#define CMD_ATTR_USR_ID        (CMD_ATTR_DEV_CLR + 1)
#define CMD_ATTR_USR_NAME      (CMD_ATTR_USR_ID + 1)
#define CMD_ATTR_USR_CLR       (CMD_ATTR_USR_NAME + 1)
#define CMD_ATTR_CMD           (CMD_ATTR_USR_CLR + 1)
#define CMD_ATTR_INFO          (CMD_ATTR_CMD + 1)
#define CMD_ATTR_TEMP          (CMD_ATTR_INFO + 1)
#define CMD_ATTR_KEEP          (CMD_ATTR_TEMP + 1)
#define CMD_ATTR_RS            (CMD_ATTR_KEEP + 1)
#define CMD_ATTR_DATE_R        (CMD_ATTR_RS + 1)
#define CMD_ATTR_DATE_S        (CMD_ATTR_DATE_R + 1)
#define CMD_ATTR_DATE          (CMD_ATTR_DATE_S + 1)
#define CMD_ATTR_LOG           (CMD_ATTR_DATE + 1)
#define CMD_ATTR_UPDATE        (CMD_ATTR_LOG + 1)
#define CMD_ATTR_OUT_MODE      (CMD_ATTR_UPDATE + 1)
#define CMD_ATTR_CLR           (CMD_ATTR_OUT_MODE + 1)
#define CMD_ATTR_COM1_BUAD     (CMD_ATTR_CLR + 1)
#define CMD_ATTR_COM1_ID       (CMD_ATTR_COM1_BUAD + 1)
#define CMD_ATTR_COM1_FO       (CMD_ATTR_COM1_ID + 1)
#define CMD_ATTR_COM2_BUAD     (CMD_ATTR_COM1_FO + 1)
#define CMD_ATTR_COM2_ID       (CMD_ATTR_COM2_BUAD + 1)
#define CMD_ATTR_COM2_FO       (CMD_ATTR_COM2_ID + 1)
#define CMD_ATTR_SIZE          (CMD_ATTR_COM2_FO + 1)
#define CMD_ATTR_END           127

#if CMD_ATTR_SIZE != 28
#error cfg cmd define error "at usb_file_cfg.c"
#endif

/*---------------------------------------------------------------------------------*
* bit 15    : not display in info.txt
* bit 14    : not display in any file
* bit 8-11  : 0 无需权限， 1-管理权限， 2-工厂权限
*----------------------------------------------------------------------------------*/
const txt_cmd_cfg_type cmd_list[] =
{
	{ 0x4000 | CMD_ATTR_ADMIN, "ADMIN.LOGIN", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback },
	{ 0x4000 | CMD_ATTR_FACTORY, "FACTORY.LOGIN", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback },
	{ 0x4100 | CMD_ATTR_PASSWORD, "PASSWORD", TXT_CMD_TYPE_STRING, cmd_back_dat, 16, cmd_callback },
	{ 0x8000 | CMD_ATTR_CMD, "CMD", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },

	{ 0x0000 | CMD_ATTR_TEMP, "TEMP", TXT_CMD_TYPE_FLOAT, cmd_back_dat, 4, cmd_callback },
	{ 0x0000 | CMD_ATTR_INFO, "INFO", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },
	{ 0x0000 | CMD_ATTR_KEEP, "KEEP", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },
	{ 0x0000 | CMD_ATTR_RS, "RS", TXT_CMD_TYPE_NUMBER, cmd_back_dat, 1, cmd_callback },
	{ 0x0000 | CMD_ATTR_DATE_R, "DATE.R", TXT_CMD_TYPE_SHORT, cmd_back_dat, 3, cmd_callback },
	{ 0x0000 | CMD_ATTR_DATE_S, "DATE.S", TXT_CMD_TYPE_SHORT, cmd_back_dat, 3, cmd_callback },
	{ 0x0000 | CMD_ATTR_DATE, "DATE", TXT_CMD_TYPE_SHORT, cmd_back_dat, 3, cmd_callback },
	{ 0x0000 | CMD_ATTR_LOG, "LOG", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },
	{ 0x0000 | CMD_ATTR_UPDATE, "UPDATE", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },

	{ 0x8200 | CMD_ATTR_DEV_ID, "DEV.ID", TXT_CMD_TYPE_STRING, cmd_back_dat, 30, cmd_callback },
	{ 0x8200 | CMD_ATTR_DEV_NAME, "DEV.NAME", TXT_CMD_TYPE_STRING, cmd_back_dat, 22, cmd_callback },
	{ 0x8200 | CMD_ATTR_DEV_CLR, "DEV.CLR", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_USR_ID, "USR.ID", TXT_CMD_TYPE_STRING, cmd_back_dat, 30, cmd_callback },
	{ 0x8100 | CMD_ATTR_USR_NAME, "USR.NAME", TXT_CMD_TYPE_STRING, cmd_back_dat, 22, cmd_callback },
	{ 0x8100 | CMD_ATTR_USR_CLR, "USR.CLR", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_CLR, "CLR", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_OUT_MODE, "OUT", TXT_CMD_TYPE_STRING, cmd_back_dat, 5, cmd_callback },
	{ 0x8100 | CMD_ATTR_COM1_BUAD, "COM1.BUAD", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_COM1_ID, "COM1.ID", TXT_CMD_TYPE_CHAR, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_COM1_FO, "COM1.FO", TXT_CMD_TYPE_CHAR, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_COM2_BUAD, "COM2.BUAD", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_COM2_ID, "COM2.ID", TXT_CMD_TYPE_CHAR, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_COM2_FO, "COM2.FO", TXT_CMD_TYPE_CHAR, cmd_back_dat, 1, cmd_callback },
	{ CMD_ATTR_END, "END", TXT_CMD_TYPE_END, cmd_back_dat, 1, cmd_callback },
};


/*******************************************************************************
* @brief  cmd_callback.
* \param[in] none
* \retval: none
*******************************************************************************/
void cmd_callback(uint16_t cmd_site, const char * txt_src, uint16_t len)
{
	char *i8p ;
	int16_t *i16p ;
	int32_t *i32p ;
	float *f32p ;
	
	if (cmd_site > sizeof(cmd_list) / sizeof(txt_cmd_cfg_type))
		return;

	i8p = cmd_list[cmd_site].des;
	i16p = cmd_list[cmd_site].des;
	i32p = cmd_list[cmd_site].des;
	f32p = cmd_list[cmd_site].des;
		
	(void)i8p;
	(void)i16p;
	(void)i32p;
	(void)f32p;	
	
	dbg_print("rcv num amount %d\n", len);
	
	switch (cmd_list[cmd_site].attr & 0xFF)
	{
		case CMD_ATTR_ADMIN:
		{
			dbg_puts("CMD_ADMIN\n");
			break;
		}
		case CMD_ATTR_FACTORY:
		{
			dbg_puts("CMD_FACTORY\n");
			break;
		}
		case CMD_ATTR_PASSWORD:
		{
			char pwd[12], pwsrc[32];
			int src_len = strlen(i8p);

			dbg_print("PASSWORD SRC LEN = \"%s\" \n", i8p);

			strcpy(pwsrc, i8p);
			pwsrc[src_len] = 0;
			string6_passward_creat(pwsrc, pwd, 1);
			dbg_print("PASSWORD 6 string = %s \n", pwd);

			strcpy(pwsrc, i8p);
			pwsrc[src_len] = 0;
			string8_passward_creat(pwsrc, pwd, 1);
			dbg_print("PASSWORD 8 string = %s \n", pwd);

			strcpy(pwsrc, i8p);
			pwsrc[src_len] = 0;
			string10_passward_creat(pwsrc, pwd, 1);
			dbg_print("PASSWORD 10 string = %s \n", pwd);

			break;
		}
		case CMD_ATTR_TEMP:
		{
			dbg_print("Temp set : %8.2f,%8.2f,%8.2f,%8.2f\n", 
					  f32p[0],
					  f32p[1],
					  f32p[2],
					  f32p[3]
					  );
			break;
		}
		case CMD_ATTR_CMD:
		{
			break;
		}
		case CMD_ATTR_KEEP:
		{
			break;
		}
		case CMD_ATTR_DEV_ID:
		{
			break;
		}
		case CMD_ATTR_DEV_NAME:
		{
			break;
		}
		case CMD_ATTR_DEV_CLR:
		{
			break;
		}
		case CMD_ATTR_USR_ID:
		{
			break;
		}
		case CMD_ATTR_USR_NAME:
		{
			break;
		}
		case CMD_ATTR_USR_CLR:
		{
			break;
		}
		case CMD_ATTR_INFO:
		{
			break;
		}
		case CMD_ATTR_RS:
		{
			break;
		}
		case CMD_ATTR_DATE_R:
		{
			break;
		}
		case CMD_ATTR_DATE_S:
		{
			break;
		}
		case CMD_ATTR_DATE:
		{
			dbg_print("DATE SET : %4d,%4d,%4d\n", 
					  i16p[0],
					  i16p[1],
					  i16p[2]
					  );
			break;
		}
		case CMD_ATTR_LOG:
		{
			break;
		}
		case CMD_ATTR_UPDATE:
		{
			break;
		}
		case CMD_ATTR_CLR:
		{
			break;
		}
		case CMD_ATTR_OUT_MODE:
		{
			break;
		}
		case CMD_ATTR_COM1_BUAD:
		{
			break;
		}
		case CMD_ATTR_COM1_ID:
		{
			break;
		}
		case CMD_ATTR_COM1_FO:
		{
			break;
		}
		case CMD_ATTR_COM2_BUAD:
		{
			break;
		}
		case CMD_ATTR_COM2_ID:
		{
			break;
		}
		case CMD_ATTR_COM2_FO:
		{
			break;
		}
		default:
		{
			break;
		}
	}
}

